package com.masterclient.app.modules.shop;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.bean.shop.DealerShippingItemBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.adapter.DealerShippingManagerAdapter;
import com.masterclient.app.modules.shop.contract.DealerShippingListContract;
import com.masterclient.app.modules.shop.contract.DealerShippingListPresenter;
import com.masterclient.app.widget.TitlebarView;
import com.gyf.immersionbar.ImmersionBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auther hjian
 * @date 2020/9/12.
 */
@Route(path = AroutePath.DEALER_SHIPPING_MANAGER_ACTIVITY)
public class DealerShippingManagerActivity extends BaseActivity<DealerShippingListPresenter> implements DealerShippingListContract.View {

    @BindView(R.id.dealer_shipping_manager_titlebar)
    TitlebarView dealerShippingManagerTitlebar;
    @BindView(R.id.dealer_shipping_manager_recycler)
    RecyclerView dealerShippingManagerRecycler;

    private DealerShippingManagerAdapter dealerShippingAdapter;
    private int clickPos;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dealer_shipping_manager);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarDarkFont(true).init();

        dealerShippingAdapter = new DealerShippingManagerAdapter();
        dealerShippingManagerRecycler.setLayoutManager(new LinearLayoutManager(this));
        dealerShippingManagerRecycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = ScreenUtils.dp2px(10);
            }
        });
        dealerShippingManagerRecycler.setAdapter(dealerShippingAdapter);
        dealerShippingAdapter.bindToRecyclerView(dealerShippingManagerRecycler);

        refreshData();

        initListener();
    }

    private void refreshData(){
        mPresenter.getShippingList("");
    }


    private void initListener() {

        dealerShippingManagerTitlebar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
            }
        });

        dealerShippingAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                clickPos = position;
                DealerShippingItemBean bean = dealerShippingAdapter.getData().get(position);
                switch (view.getId()){
                    case R.id.item_dealer_shipping_select:
                    case R.id.item_dealer_shipping_default_text:
                        if ("0".equals(bean.getIs_default())) {
                            mPresenter.setDefault(bean.getDelivery_id());
                        }
                        break;
                    case R.id.item_dealer_shipping_preview:
                        ARouter.getInstance().build(AroutePath.DEALER_SHIPPING_DETAIL_ACTIVITY)
                                .withString("id", bean.getDelivery_id()).navigation();
                        break;
                }
            }
        });

    }

    @Override
    public void showData(List<DealerShippingItemBean> dataBean) {
        if (dataBean!= null){
            dealerShippingAdapter.setNewData(dataBean);
        }
    }

    @Override
    public void showFailure(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public void defaultResult(boolean isSuccess) {
        if (isSuccess) {
            setResult(RESULT_OK);
            List<DealerShippingItemBean> bean = dealerShippingAdapter.getData();
            if (dealerShippingAdapter.defaultPos != -1){
                bean.get(dealerShippingAdapter.defaultPos).setIs_default("0");
                bean.get(clickPos).setIs_default("1");
                dealerShippingAdapter.defaultPos = clickPos;
                dealerShippingAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void showSelectResult(boolean isSuccess) {

    }
}
